package com.jyu.apts.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.jyu.apts.dto.ItemProductionLineDto;
import com.jyu.apts.dto.ResultDto;
import com.jyu.apts.dto.TableDto;
import com.jyu.apts.dto.UserDto;
import com.jyu.apts.entity.Item;
import com.jyu.apts.entity.ItemProductionLine;
import com.jyu.apts.entity.Organization;
import com.jyu.apts.entity.User;
import com.jyu.apts.service.ItemProductionLineService;
import com.jyu.apts.service.ItemService;
import com.jyu.apts.service.OrganizationService;
import com.jyu.apts.service.UserService;

@RestController
public class OrganizationItemProductionLineController {

	@Autowired
	ItemProductionLineService itemProductionLineService;
	@Autowired
	ItemService itemService;
	@Autowired
	OrganizationService organizationService;
	@Autowired 
	UserService userService;
	
	@RequiresPermissions("organizationItemProductionLine_html")
	@RequestMapping("/organizationItemProductionLine_html")
	public ModelAndView organizationItemProductionLine_html() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/organizationItemProductionLine.html");
		return mv;
	}
	
	@RequiresPermissions("organizationItemProductionLine_html")
	@RequestMapping("/addOrganizationItemProductionLine")
	public ResultDto addOrganizationItemProductionLine(String name,long itemId) {
		Item item = itemService.getOne(itemId);
		if(item == null) {
			return new ResultDto("商品不存在",null,2);
		}
		UserDto userDto = (UserDto) SecurityUtils.getSubject().getSession().getAttribute("userDto");
		User orgAdmin = userService.getOne(userDto.getId());
		if(item.getOrganization().getId()!=orgAdmin.getOrganization().getId()) {
			return new ResultDto("非本机构商品",null,2);
		}
		ItemProductionLine itemProductionLine = new ItemProductionLine();
		itemProductionLine.setName(name);
		itemProductionLine.setItem(item);
		itemProductionLineService.save(itemProductionLine);
		return new ResultDto("添加成功",null,0);
	}
	
	@RequiresPermissions("organizationItemProductionLine_html")
	@RequestMapping("/upOrganizationItemProductionLine")
	public ResultDto upOrganizationItemProductionLine(long id,String name,long itemId) {
		ItemProductionLine itemProductionLine = itemProductionLineService.getOne(id);
		if(itemProductionLine == null) {
			return new ResultDto("商品生产线不存在!",null,2);
		}
		Item item = itemService.getOne(itemId);
		if(item == null) {
			return new ResultDto("商品不存在",null,2);
		}
		UserDto userDto = (UserDto) SecurityUtils.getSubject().getSession().getAttribute("userDto");
		User orgAdmin = userService.getOne(userDto.getId());
		if(item.getOrganization().getId()!=orgAdmin.getOrganization().getId()) {
			return new ResultDto("非本机构商品",null,2);
		}
		itemProductionLine.setItem(item);
		itemProductionLine.setName(name);
		itemProductionLineService.save(itemProductionLine);
		return new ResultDto("更新成功",null,0);
	}
	
	@RequiresPermissions("organizationItemProductionLine_html")
	@RequestMapping("/delOrganizationItemProductionLine")
	public ResultDto delOrganizationItemProductionLine(String ids) {
		String[] idArray = ids.split(",");
		List<Long> l_ids = new ArrayList<Long>();
		for(String id : idArray) {
			l_ids.add(new Long(id));
		}
		UserDto userDto = (UserDto) SecurityUtils.getSubject().getSession().getAttribute("userDto");
		User orgAdmin = userService.getOne(userDto.getId());
		Organization organization = orgAdmin.getOrganization();
		List<ItemProductionLine> itemProductionLines = itemProductionLineService.findAllById(l_ids);
		for(ItemProductionLine itemProductionLine : itemProductionLines) {
			if(itemProductionLine.getItem().getOrganization().getId()!=organization.getId()) {
				return new ResultDto("存在非本机构商品生产线",null,2);
			}
		}
		itemProductionLineService.delete(itemProductionLines);
		return new ResultDto("删除成功",null,0);
	}
	
	@RequiresPermissions("organizationItemProductionLine_html")
	@RequestMapping("/getOrganizationItemProductionLinePage")
	public TableDto<ItemProductionLineDto> getOrganizationItemProductionLinePage(Integer pageNumber,Integer pageSize,Long itemId,String search) {
		UserDto userDto = (UserDto) SecurityUtils.getSubject().getSession().getAttribute("userDto");
		User orgAdmin = userService.getOne(userDto.getId());
		Organization organization = orgAdmin.getOrganization();
		Page<ItemProductionLine> pageResult = itemProductionLineService.getItemProductionLinePage(pageNumber, pageSize, organization.getId(), itemId, search);
		List<ItemProductionLine> itemProductionLines = pageResult.getContent();
		List<ItemProductionLineDto> itemProductionLineDtos = new ArrayList<ItemProductionLineDto>();
		itemProductionLines.forEach(itemProductionLine->itemProductionLineDtos.add(new ItemProductionLineDto().convert(itemProductionLine)));
		return new TableDto<ItemProductionLineDto>(itemProductionLineDtos,pageResult.getTotalElements());
	}
	
	@RequiresPermissions("organizationItemProductionLine_html")
	@RequestMapping("/getAllOrganizationItemProductionLineByItem")
	public List<ItemProductionLineDto> getAllOrganizationItemProductionLineByItem(long itemId) {
		Item item = itemService.getOne(itemId);
		UserDto userDto = (UserDto) SecurityUtils.getSubject().getSession().getAttribute("userDto");
		User orgAdmin = userService.getOne(userDto.getId());
		Organization organization = orgAdmin.getOrganization();
		if(item.getOrganization().getId()!=organization.getId()) 
			return null;
		List<ItemProductionLineDto> itemProductionLineDtos = new ArrayList<ItemProductionLineDto>();
		List<ItemProductionLine> itemProductionLines = item.getItemProductionLines();
		itemProductionLines.forEach(itemProductionLine->itemProductionLineDtos.add(new ItemProductionLineDto().convert(itemProductionLine)));
		return itemProductionLineDtos;
	}
	
}
